package com.xcc.mota.utils;

import android.content.Context;
import android.graphics.Bitmap;

/**
 * Created by Administrator on 2018/1/19.
 */

public class BitmapLoad {
    private static BitmapLoad bitmapLoad;

    public static synchronized BitmapLoad getInstance(Context context) {
        if (bitmapLoad == null) bitmapLoad = new BitmapLoad(context);
        return bitmapLoad;
    }

    private BitmapLoad(Context context) {
        AssetsLoad assetsLoad = new AssetsLoad(context);
        imgall = assetsLoad.getBitmap("imgall.png");
        imgalls = new Bitmap[24][16];
        myactor = assetsLoad.getBitmap("myactor.png");
        npcimg = assetsLoad.getBitmap("npcimg.png");
        wupin = assetsLoad.getBitmap("npcimg.png");
    }

    //32x32
    //17x22+9
    private Bitmap imgall;
    private Bitmap imgalls[][];
    private Bitmap myactor;
    private Bitmap myactors[][];
    private Bitmap npcimg;
    private Bitmap npcimgs[][];
    private Bitmap wupin;
    private Bitmap wupins[][];

    public Bitmap getBitmap(int type, int idx) {
        Bitmap bitmap = null;
        if (type == 0) {
            bitmap = getImgall(idx);
        } else if (type == 1) {
            bitmap = getWupin(idx);
        }
        return bitmap;
    }

    /**
     * @param idx xy-> 00 00
     */
    private Bitmap getImgall(int idx) {
        int x = idx / 100;
        int y = idx % 100;
        Bitmap bitmap = imgalls[x][y];
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(imgall, x * 32, y * 32, 32, 32);
            imgalls[x][y] = bitmap;
        }
        return bitmap;
    }

    private Bitmap getWupin(int idx) {
        int x = idx / 100;
        int y = idx % 100;
        Bitmap bitmap = wupins[x][y];
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(wupin, x * 32, y * 32, 32, 32);
            wupins[x][y] = bitmap;
        }
        return bitmap;
    }

    public Bitmap[][] getMyactors() {
        if (myactors == null) {
            myactors = new Bitmap[4][3];
            for (int i = 0; i < myactors.length; i++) {
                Bitmap[] myactor_ = new Bitmap[3];
                myactors[i] = myactor_;
                for (int j = 0; j < myactor_.length; j++) {
                    myactor_[j] = Bitmap.createBitmap(myactor, j * 32, i * 32, 32, 32);
                }
            }
        }
        return myactors;
    }

    public Bitmap[][] getNpcimg() {
        if (npcimgs == null) {
            npcimgs = new Bitmap[7][3];
            for (int i = 0; i < npcimgs.length; i++) {
                Bitmap[] myactor_ = new Bitmap[3];
                npcimgs[i] = myactor_;
                for (int j = 0; j < myactor_.length; j++) {
                    myactor_[j] = Bitmap.createBitmap(npcimg, j * 32, i * 32, 32, 32);
                }
            }
        }
        return npcimgs;
    }
}
